package com.starbooks.tmail.controller;

import com.starbooks.tmail.dto.ProfileForm;

import com.starbooks.tmail.po.User;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import org.springframework.validation.Errors;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author starbooks
 */
@Controller
public class HomeController {

  @GetMapping({"/", "/home"})
  public String homePage(
      HttpSession session,
      Model model,
      @RequestParam(defaultValue = "0") String id){
    model.addAttribute("app", "adidas");
    model.addAttribute("query", id);
    model.addAttribute("appID", session.getAttribute("appID"));
    return "home";
  }

  @PostMapping("/profile")
  public String handlePostProfile(@Valid ProfileForm profileForm, Errors errors){
    System.out.println(profileForm);
    if(errors.hasErrors()){
      System.out.println(errors);
      return "profile";
    }
    return "home";
  }

  @GetMapping("/profile")
  public String handleGetProfile(ProfileForm profileForm){
    return "profile";
  }
}

@RestController
class TestController {

  @Value("${author.name}")
  private String name;

  @GetMapping({"/test1", "/test2"})
  public String test1(){
    return name;
  }

  @GetMapping("/test3")
  public String test3(int[] ids){
    System.out.println(ids[0]);
    System.out.println(ids[1]);
    return name;
  }

  @PostMapping("/test4")
  public String test4(@RequestBody User user){
    System.out.println(user);
    return "ok";
  }

  // form
  @PostMapping("/test5")
  public String test5(@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date date){
    System.out.println(date);
    return "ok";
  }

}
